require(['goSwiper'], function(goSwiper) {
/*	//获取定位信息 默认定位到北京市中心
	var longitude = 116.403963;
	var latitude = 39.915119;
	var city = '北京市';*/
	var aPageNo = 1;
	var fPageNo = 1;
	var zPageNo = 1;
	var xPageNo = 1;
	var pageSize = 10;
	var aflag = true;
	var fflag = true;
	var zflag = true;
	var xflag = true;
	var acanscroll = false;
	var fcanscroll = false;
	var zcanscroll = false;
	var xcanscroll = false;
	getAllFarm();
	getNearbyFarm();
	getPlantFarm();
	getLookFarm();
	
	//点击搜索
    $('#goSearch').on('click', function() {
    	window.location.href = '../yellowPage/farmSearch.html';
    });
    
	//切换
	$(".fq_diyStart").on("click",function() {
		$(this).addClass("fq_active").siblings().removeClass("fq_active");
		$(this).children("i").addClass("fq_border").parent().siblings().children("i").removeClass("fq_border");
		var index = $(this).index();
		$(".products > div")
			.eq(index)
			.addClass("fq_selected")
			.siblings("div")
			.removeClass("fq_selected");
		$("html,body").animate({
			scrollTop: 0
		}, 500);
	});
	//获取全部农庄
	function getAllFarm(){
		xget(
			wxUrl + "/v3.2/farm/yellowPage/list",
			{
				token : token,
		    	pageNo : aPageNo,
		    	pageSize : 20,
		    	type : '0',
		    	city : city,
		    	keyword : '',
		    	lat : latitude,
		    	lng : longitude
			},
			function(data){
				$('.loading_modal').hide();
	            if (data.code != 0) {
					promptBox(data.msg);
	            } else {
	            	var result = data.data;
					acanscroll = true;
					var data2 = result.farms;
					var data3 = {
	                		tem : data2
	                    };
					if(data2.length > 0){
						var html = _.template($('#allFarmTmp').html(),data3);
	            		$("#allFarm").append(html);
	            		$('.swiper-wrapper').height($('.swiper-slide-active').height());
	            		aPageNo++;
					}else{
						if(aPageNo > 1){
							acanscroll = false;
							promptBox('没有更多数据了！');
						}else{
							aflag = false;
							var html = '';
							    html += '<div class="no_data">';
							    html += '<img src="../static/images/defaultImg1.png" />';
							    html += '<p>暂无相关信息</p>';
							    html += '</div>';
							$("#allFarm").append(html);
						}
					}
					//点击进入全部农庄详情
					$('.wz_newLi11').on("click",function(){
						var liId = $(this).attr('key');
						window.location.href = '../yellowPage/yellowPage_detail.html?id=' + liId;
					});
				}
			}
		);
	};

	//获取附近农庄
	function getNearbyFarm(){
		xget(
			wxUrl + "/v3.2/farm/yellowPage/list",
			{
				token : token,
		    	pageNo : fPageNo,
		    	pageSize : 20,
		    	type : '1',
		    	city : city,
		    	keyword : '',
		    	lat : latitude,
		    	lng : longitude
			},
			function(data){
				$('.loading_modal').hide();
	            if (data.code != 0) {
					promptBox(data.msg);
	            } else {
	            	var result = data.data;
					fcanscroll = true;
					var data2 = result.farms;
					var data3 = {
	                		tem : data2
	                    };
					if(data2.length > 0){
						var html = _.template($('#nearbyFarmTmp').html(),data3);
	            		$("#nearbyFarm").append(html);
						$('.swiper-wrapper').height($('.swiper-slide-active').height());
						fPageNo++;
					}else{
						if(fPageNo > 1){
							fcanscroll = false;
							promptBox('没有更多数据了！');
						}else{
							fflag = false;
							var html = '';
							    html += '<div class="no_data">';
							    html += '<img src="../static/images/defaultImg1.png" />';
							    html += '<p>暂无相关信息</p>';
							    html += '</div>';
							$("#nearbyFarm").append(html);
						}
					}
					//点击进入全部农庄详情
					$('.wz_newLi22').on("click",function(){
						var liId = $(this).attr('key');
						window.location.href = '../yellowPage/yellowPage_detail.html?id=' + liId;
					});
				}
			}
		);
	};

	//获取种植农庄
	function getPlantFarm(){
		xget(
			wxUrl + "/v3.2/farm/yellowPage/list",
			{
				token : token,
		    	pageNo : zPageNo,
		    	pageSize : 20,
		    	type : '2',
		    	city : city,
		    	keyword : '',
		    	lat : latitude,
		    	lng : longitude
			},
			function(data){
				$('.loading_modal').hide();
	            if (data.code != 0) {
					promptBox(data.msg);
	            } else {
	            	var result = data.data;
					zcanscroll = true;
					var data2 = result.farms;
					var data3 = {
	                		tem : data2
	                    };
					if(data2.length > 0){
						var html = _.template($('#plantFarmTmp').html(),data3);
	            		$("#plantFarm").append(html);
						$('.swiper-wrapper').height($('.swiper-slide-active').height());
						zPageNo++;
					}else{
						if(zPageNo > 1){
							zcanScroll = false;
							promptBox('没有更多数据了！');
						}else{
							zflag = false;
							var html = '';
							    html += '<div class="no_data">';
							    html += '<img src="../static/images/defaultImg1.png" />';
							    html += '<p>暂无相关信息</p>';
							    html += '</div>';
							$("#plantFarm").append(html);
						}
					}
					//点击进入种植农庄
					$('.wz_newLi33').on("click",function(){
						var liId = $(this).attr('key');
						window.location.href = '../yellowPage/yellowPage_detail.html?id=' + liId;
					});
				}
			}
		);
	};
	//获取休闲农庄
	function getLookFarm(){
		xget(
			wxUrl + "/v3.2/farm/yellowPage/list",
			{
				token : token,
		    	pageNo : xPageNo,
		    	pageSize : 20,
		    	type : '3',
		    	city : city,
		    	keyword : '',
		    	lat : latitude,
		    	lng : longitude
			},
			function(data){
				$('.loading_modal').hide();
	            if (data.code != 0) {
					promptBox(data.msg);
	            } else {
	            	var result = data.data;
					xcanscroll = true;
					var data2 = result.farms;
					var data3 = {
	                		tem : data2
	                    };
					if(data2.length > 0){
						var html = _.template($('#lookFarmTmp').html(),data3);
	            		$("#lookFarm").append(html);
						$('.swiper-wrapper').height($('.swiper-slide-active').height());
						xPageNo++;
					}else{
						if(xPageNo > 1){
							xcanScroll = false;
							promptBox('没有更多数据了！');
						}else{
							xflag = false;
							var html = '';
							    html += '<div class="no_data">';
							    html += '<img src="../static/images/defaultImg1.png" />';
							    html += '<p>暂无相关信息</p>';
							    html += '</div>';
							$("#lookFarm").append(html);
						}
					}
					//点击进入休闲农庄
					$('.wz_newLi44').on("click",function(){
						var liId = $(this).attr('key');
						window.location.href = '../yellowPage/yellowPage_detail.html?id=' + liId;
					});
				}
			}
		);
	};
	//当滚动条到底部时加载更多
	$(window).scroll(function(){
		var scrollTop = $(window).scrollTop();
		var scrollHeight = $(document).height();
		var windowHeight = $(window).height();
		var divId = $('.fq_selected').attr('id');
		if(scrollTop + windowHeight == scrollHeight){
			if(divId == 'allFarm' && aflag && acanscroll){
				getAllFarm(); //pageNo++
				acanscroll = false;
			}else if(divId == 'nearbyFarm' && fflag && fcanscroll){
				getNearbyFarm();
				fcanscroll = false;
			}else if(divId == 'plantFarm' && zflag && zcanscroll){
				getPlantFarm();
				zcanscroll = false;
			}else if(divId == 'lookFarm' && xflag && xcanscroll){
				getLookFarm();
				xcanscroll = false;
			}
		}
	});
});
// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
	goBackVHistory();
});